package com.lvmama.service.nps.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lvmama.common.enums.EnumForCategory;
import com.lvmama.common.enums.OrderTypeEnum;
import com.lvmama.common.result.Result;
import com.lvmama.common.utils.DateUtil;
import com.lvmama.dao.nps.NPSCommitDao;
import com.lvmama.pojo.dto.nps.NPSCommitDTO;
import com.lvmama.pojo.entity.nps.NPSCommit;
import com.lvmama.pojo.entity.nps.NPSStatistics;
import com.lvmama.service.nps.NpsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class NpsServiceImpl implements NpsService {

    /**
     *
     */
    @Autowired
    private NPSCommitDao npsCommitDao;


    /**
     * 查询提交的nps问卷
     *
     * @param query 查询条件
     * @return 结果分页
     */
    @Override
    public Result<PageInfo<NPSCommit>> selectNpsCommitList(NPSCommitDTO query) {
        log.info("queryNPSCommitPage start.");
        //设置分页
        PageHelper.startPage(query.getCurrentPage(), query.getPageSize());
        PageHelper.orderBy(query.getOrderBy());
        List<NPSCommit> npsCommits = null;
        try {
            npsCommits = queryNPSCommitList(query).getData();
        } catch (Exception e) {
            log.error("queryNPSCommitPage error:" + e.getMessage(), e);
        }
        log.info("queryNPSCommitPage end.");
        //return new PageInfo<>(npsCommits);
        return Result.success(new PageInfo<>(npsCommits));
    }


    /**
     * 查询提交的nps问卷
     *
     * @param query 查询条件
     * @return 结果集
     */
    @Override
    public Result<List<NPSCommit>>  queryNPSCommitList(NPSCommitDTO query) {
        log.info("queryNPSCommitList start.");
        List<NPSCommit> npsCommits = null;
        try {
            npsCommits = npsCommitDao.selectNpsCommitList(query);
            npsCommitConverter(npsCommits, query.getNpsQuestionType());
        } catch (Exception e) {
            log.error("queryNPSCommitList error:" + e.getMessage(), e);
        }
        log.info("queryNPSCommitList end.");
        return Result.success(npsCommits);
    }

    /**
     * 查询提交的nps内部问卷
     *
     * @param query 查询条件
     * @return 结果分页
     */
    @Override
    public Result<PageInfo<NPSCommit>> queryNPSInternalCommitPage(NPSCommitDTO query) {
        log.info("queryNPSInternalCommitPage start.");
        //设置分页
        PageHelper.startPage(query.getCurrentPage(), query.getPageSize());
        PageHelper.orderBy(query.getOrderBy());
        List<NPSCommit> npsCommits = null;
        try {
            npsCommits = queryNPSInternalCommitList(query).getData();
        } catch (Exception e) {
            log.error("queryNPSInternalCommitPage error:" + e.getMessage(), e);
        }
        log.info("queryNPSInternalCommitPage end.");
        return Result.success(new PageInfo<>(npsCommits));
    }

    @Override
    public Result<List<NPSCommit>> queryNPSInternalCommitList(NPSCommitDTO query) {
        log.info("queryNPSInternalCommitList start.");
        List<NPSCommit> npsCommits = null;
        try {
            npsCommits = npsCommitDao.queryNPSInternalCommitList(query);
            npsCommitConverter(npsCommits, query.getNpsQuestionType());
        } catch (Exception e) {
            log.error("queryNPSInternalCommitList error:" + e.getMessage(), e);
        }
        log.info("queryNPSInternalCommitList end.");
        return Result.success(npsCommits);
    }

    /**
     *  查询nps后台统计信息
     * @param query 查询条件
     * @return 结果
     */
    @Override
    public Result<List<NPSStatistics>> queryNpsStatistics(NPSCommitDTO query) {
        log.info("queryNpsStatistics start.");
        List<NPSStatistics> npsStatistics = null;
        try {
            npsStatistics = npsCommitDao.queryNpsStatistics(query);
            if (npsStatistics != null && !npsStatistics.isEmpty()) {
                for (NPSStatistics npsStat : npsStatistics){
                    if(query.isGroupByCategory()){
                        npsStat.setCategoryName(EnumForCategory.getCategoryShowNameByValue(npsStat.getCategory()));
                    }
                    if(query.isGroupCommitType()){
                        OrderTypeEnum orderTypeEnum = OrderTypeEnum.getEnum(npsStat.getCommitType());
                        npsStat.setCommitTypeName(orderTypeEnum==null? npsStat.getCommitType() : orderTypeEnum.getShowName());
                    }
                    if(null == npsStat.getNps()){
                        npsStat.setNps(0F);
                    }
                }
            }
            //添加总计
            if((query.isGroupByCategory() && null != npsStatistics && npsStatistics.size()>1) || query.isGroupCommitType()){
                query.setGroupByCategory(false);
                query.setGroupCommitType(false);
                NPSStatistics npsStatis = npsCommitDao.queryNpsStatistics(query).get(0);
                npsStatis.setCategoryName("总计");
                npsStatis.setCommitTypeName("总计");
                if(npsStatistics!=null){
                    npsStatistics.add(npsStatis);
                }
            }
        } catch (Exception e) {
            log.error("queryNpsStatistics error:" + e.getMessage(), e);
        }
        log.info("queryNpsStatistics end.");
        return Result.success(npsStatistics);
    }

    /**
     *  查询nps内部后台统计信息
     * @param query 查询条件
     * @return 结果
     */
    @Override
    public Result<NPSStatistics> queryNpsInternalStatistics(NPSCommitDTO query) {
        log.info("queryNpsInternalStatistics start.");
        NPSStatistics npsStatistics = null;
        try {
            npsStatistics = npsCommitDao.queryNpsInternalStatistics(query);
            if (npsStatistics != null && npsStatistics.getNps() == null) {
                npsStatistics.setNps(0F);
            }
        } catch (Exception e) {
            log.error("queryNpsInternalStatistics error:" + e.getMessage(), e);
        }
        log.info("queryNpsInternalStatistics end.");
        return Result.success(npsStatistics);
    }


    private void npsCommitConverter(List<NPSCommit> npsCommits, String npsQuestionType) {
        for (NPSCommit commit : npsCommits) {
            commit.setCommitTypeCode(commit.getCommitType());
            //枚举转换
            commit.setCategoryName(EnumForCategory.getCategoryShowNameByValue
                    (String.valueOf(commit.getCategoryId())));
            //时期转换
            commit.setCommitTime(DateUtil.dateFormat(commit.getCreateTime(), DateUtil.PATTERN_YYYY_MM_DD_HHMMSS));
            if (commit.getScore() != null) {
                String socreStyle = null;
                if (commit.getScore() < 7) {
                    socreStyle = "<div style=\"background-color: #666666;" +
                            "width: 100%;height: 22px;color: #fff;\">" + commit.getScore() + "</div>";
                } else if (commit.getScore() > 6 && commit.getScore() < 9) {
                    socreStyle = "<div style=\"background-color: #5CA2F8;" +
                            "width: 100%;height: 22px;color: #fff;\">" + commit.getScore() + "</div>";
                } else if (commit.getScore() > 8) {
                    socreStyle = "<div style=\"background-color: #FE3C71;" +
                            "width: 100%;height: 22px;color: #fff;\">" + commit.getScore() + "</div>";
                }
                commit.setScoreStyle(socreStyle);
            }

            OrderTypeEnum orderTypeEnum = OrderTypeEnum.getEnum(commit.getCommitType());
            if (null != orderTypeEnum) {
                commit.setCommitType(orderTypeEnum.getShowName());
            } else {
                commit.setCommitType(commit.getCommitType()==null ? "" : commit.getCommitType());
            }
            if (null == commit.getUserId()) {
                commit.setUserId("");
            }
            if (null == commit.getPhoneNumber()) {
                commit.setPhoneNumber("");
            }
            if (null == commit.getOrderId()) {
                commit.setOrderId("");
            }
            if (null == commit.getSurveyNo()) {
                commit.setSurveyNo("");
            }
            commit.setNpsQuestionType(npsQuestionType);
            commit.setOriginRemark(commit.getRemark());
            if (StringUtils.isNotEmpty(commit.getRemark()) && commit.getRemark().length() > 30) {
                String remark = commit.getRemark().substring(0, 30) + "<a title='" + commit.getRemark()
                        + "' data-rel='tooltip' href='javascript:void(0)' class='tooltip-info'>...</a>";
                commit.setRemark(remark);
            } else {
                commit.setRemark(commit.getRemark() == null ? "" : commit.getRemark());
            }
        }
    }



}
